﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication4
{
    
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{

        public interface clickableInterface
        {
            void leftDown();
        }
        public enum Actions {Clickobject, Moveobject, Addaduc, Addclk, Addconnection, Deleteobject}; // not easy to scale
        public UIElement MovableElement = null;
        public Actions actionOnClick;

        public double xx;
        public Line l;
        public static Wire StaticTempWire = null;
        public static Line StaticTempLine = null;

        
		public Window1()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
            Core_controlxaml cc  = new Core_controlxaml();
            Core_controlxaml cc1 = new Core_controlxaml();
          //  can1.Children.Add(cc);
          //  can1.Children.Add(cc1);
            //can1.Children.Add(cc);
            l = new Line();

            l.X1 = 0;
            l.X2 = 550;
            l.Y1 = 0;
            l.Y2 = 500;
            l.Stroke = Brushes.Black;
            //l.StrokeThickness = 1;
            can1.Children.Insert(0, l);
            
		}

        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            /*
            Image tmp = new Image();
            if (sender.GetType() != tmp.GetType() ) return;
            Image Img = (Image)sender;
            
            xx++;
            Point p = e.GetPosition(can1);

            Double d = p.X - Img.Width / 2;
            d = (d>=0)? d : 0;
            Double dd = p.Y - Img.Height / 2;
            dd = (dd>=0)? dd : 0;
            Canvas.SetLeft(Img, d);
            Canvas.SetTop(Img, dd);
            //Window1.TitleProperty =
            
            String s = d.ToString();
            Window.Title = s;
            //can1.Children.Add();
             */
            if (actionOnClick == Actions.Moveobject && MovableElement != null)
            {
                Point p = e.GetPosition(can1);
                Double d = p.X;// - MovableElement. / 2;
                d = (d >= 0) ? d : 0;
                Double dd = p.Y;// - MovableElement.Height / 2;
                dd = (dd >= 0) ? dd : 0;
                Canvas.SetLeft(MovableElement, d);
                Canvas.SetTop(MovableElement, dd);
            }
        }







        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            
            /*
            xx++;
            Point p = e.GetPosition(can1);
            Double d = p.X - button1.Width / 2;
            d = (d >= 0) ? d : 0;
            Double dd = p.Y - button1.Height / 2;
            dd = (dd >= 0) ? dd : 0;
            Canvas.SetLeft(button1, d);
            Canvas.SetTop(button1, dd);


            l.Y2--;
            */

        }

        public  UIElement getHighLevelUIElement(object x)
        {
            if (x == can1) return null;
            // Get High level UIElement, execpt Canvas
            DependencyObject HLElem = (DependencyObject)x;
            // while HLElem.Parent != canvas
            while (VisualTreeHelper.GetParent(HLElem = VisualTreeHelper.GetParent(HLElem)) != (DependencyObject)can1) ;
            return (UIElement)HLElem;
        }
        private void can1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            switch (actionOnClick)
            {
                case Actions.Moveobject:
                    if (MovableElement == null)
                        MovableElement = getHighLevelUIElement(e.Source);
                    else MovableElement = null;
                    break;
                case Actions.Addaduc:
                    aduc.Aduc x = new aduc.Aduc();
                    can1.Children.Add(x);
                    Canvas.SetLeft(x, e.GetPosition(can1).X);
                    Canvas.SetTop(x, e.GetPosition(can1).Y);
                    break;
                case Actions.Addclk:
                    ClockGenerator x2 = new ClockGenerator();
                    can1.Children.Add(x2);
                    Canvas.SetLeft(x2, e.GetPosition(can1).X);
                    Canvas.SetTop(x2, e.GetPosition(can1).Y);
                    break;
                case Actions.Addconnection:
                    if (!(e.Source is ExternalConnector)) return;
                    if (StaticTempWire == null)
                    {
                        ExternalConnector ext = (ExternalConnector)e.Source;
                        StaticTempWire = new Wire();
                        ext.connectWire(StaticTempWire);
                        StaticTempWire.registerListener(ext);
                        StaticTempLine = new Line();
                        StaticTempLine.Stroke = Brushes.Black;
                        StaticTempLine.X1 = Canvas.GetLeft(getHighLevelUIElement(ext));
                        StaticTempLine.Y1 = Canvas.GetTop(getHighLevelUIElement(ext));
                    }
                    else
                    {
                        ExternalConnector ext1 = (ExternalConnector)e.Source;
                        StaticTempWire.registerListener(ext1);
                        can1.Children.Add(StaticTempWire);
                        //UIElement ff = getHighLevelUIElement(ext1);
                        StaticTempLine.X2 = Canvas.GetLeft(getHighLevelUIElement(ext1));
                        StaticTempLine.Y2 = Canvas.GetTop(getHighLevelUIElement(ext1));
                        can1.Children.Add(StaticTempLine); // temporary easy drawing a line
                        StaticTempLine = null;
                        StaticTempWire = null;
                    }
                    
                    break;
                case Actions.Deleteobject:
                    can1.Children.Remove(getHighLevelUIElement(e.Source));
                    // @TODO: deleting is MORE complex. See for example Wire
                    break;
                case Actions.Clickobject:
                    UIElement t2 = getHighLevelUIElement(e.Source);
                    clickableInterface tt = (clickableInterface)t2;
                    tt.leftDown();                    
                    break;

            }
        }

        private void Img_Copy_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            actionOnClick = Actions.Addaduc;
        }
        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            actionOnClick = Actions.Deleteobject;
        }

        private void Image_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
        {
            actionOnClick = Actions.Moveobject;
        }

        private void Image_MouseLeftButtonUp_2(object sender, MouseButtonEventArgs e)
        {
            actionOnClick = Actions.Addconnection;
        }

        private void Image_MouseLeftButtonUp_3(object sender, MouseButtonEventArgs e)
        {
            actionOnClick = Actions.Addclk;
        }

        private void Image_MouseLeftButtonUp_4(object sender, MouseButtonEventArgs e)
        {
            actionOnClick = Actions.Clickobject;
        }



 


        
	}
}